package com.pms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pms.constant.MessageConstant;
import com.pms.dto.PaymentPageQueryDTO;
import com.pms.dto.PaymentSaveDTO;
import com.pms.dto.PaymentUpdateDTO;
import com.pms.entity.Owner;
import com.pms.entity.Payment;
import com.pms.exception.NotFoundException;
import com.pms.mapper.OwnerMapper;
import com.pms.mapper.PaymentMapper;
import com.pms.result.PageResult;
import com.pms.service.PaymentService;
import com.pms.vo.PaymentPageQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentMapper paymentMapper;
    @Autowired
    private OwnerMapper ownerMapper;

    /**
     * 新增缴费信息
     * @param paymentSaveDTO
     */
    public void save(PaymentSaveDTO paymentSaveDTO) {
        Owner owner=ownerMapper.getByNameAndPhone(paymentSaveDTO.getOwnerName(),paymentSaveDTO.getPhone());
        if(owner==null){
            throw new NotFoundException(MessageConstant.OWNER_NOT_EXIST);
        }

        Payment payment = new Payment();
        BeanUtils.copyProperties(paymentSaveDTO,payment);
        payment.setOwnerId(owner.getId());
        payment.setCreateDate(LocalDate.now());

        paymentMapper.save(payment);
    }

    /**
     * 缴费信息分页查询
     * @param paymentPageQueryDTO
     * @return
     */
    public PageResult page(PaymentPageQueryDTO paymentPageQueryDTO) {
        PageHelper.startPage(paymentPageQueryDTO.getPage(), paymentPageQueryDTO.getPageSize());
        Page<PaymentPageQueryVO> page=paymentMapper.page(paymentPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 修改缴费信息
     * @param paymentUpdateDTO
     */
    public void update(PaymentUpdateDTO paymentUpdateDTO) {
        Payment payment=paymentMapper.getById(paymentUpdateDTO.getId());
        payment.setStatusId(paymentUpdateDTO.getStatusId());
        paymentMapper.update(payment);
    }

    /**
     * 删除缴费信息
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            paymentMapper.delete(id);
        }
    }
}
